package com.st.emps.staff.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.st.emps.department.model.DepartmentModel;
import com.st.emps.staff.model.StaffModel;
import com.st.emps.util.JDBCUtil;


public class StaffDao {

	
	public int insert(StaffModel model) {
		String sql = "insert into staff(code,name,password) values(?,?,?)";
		return JDBCUtil.update(sql,model.getCode(),model.getName(),model.getPassword());
	}
	public int delete(StaffModel model) {
	    String sql = "delete from staff where code =?";
	    return JDBCUtil.update(sql,model.getCode());

	}
	public int update(StaffModel model) {
		String sql = "update staff set name = ?,sex=?,age=? where code= ?";
        return JDBCUtil.update(sql, model.getName(),model.getSex(),model.getAge(),model.getCode());
	}
	public List<StaffModel> select(StaffModel model) {
		StringBuffer sql = new StringBuffer("select code,name,sex,age,password,department_id,(select name from department where code = department_id) department_name ,image from staff where 1=1 ");
        List<Object> vals = appendWhere(sql,model);
        Map<String,String> fields = new HashMap<String,String>();
        fields.put("code", "code");
        fields.put("name", "name");
        fields.put("sex","sex");
        fields.put("age", "age");
        fields.put("password", "password");
        fields.put("departmentId", "department_id");
        fields.put("departmentName", "department_name");
        fields.put("image", "image");
        return JDBCUtil.query(sql,vals,StaffModel.class,fields);
	}
	private List<Object> appendWhere(StringBuffer sql,StaffModel model) {
		List<Object> list = new ArrayList<Object>();
		String code = model.getCode();
		if(code!=null&&!code.trim().isEmpty()) {
			sql.append(" and code like ? ");
			list.add(code);
		}
		String name = model.getName();
		if(name!=null&&!name.trim().isEmpty()) {
			sql.append(" and name like ? ");
			list.add(name);
		}
		if(model.isPageOn()) {
			sql.append(" limit ?,? ");
			list.add(model.getRowStart());
			list.add(model.getPageLimit());
		}
		return list;
	}
	public Object selectCount(StaffModel model) {
		StringBuffer sql = new StringBuffer("select count(1) from staff where 1=1 ");
		List<Object> list = appendWhere(sql,model);
		return JDBCUtil.queryInt(sql,list);
	}
	public int updatePassword(StaffModel model) {
		String sql = "update staff set password=? where code= ?";
        return JDBCUtil.update(sql, model.getPassword(),model.getCode());
	}
	public int updateDepart(StaffModel model) {
		String sql = "update staff set department_id = ? where code = ? ";
		return JDBCUtil.update(sql, model.getDepartmentId(),model.getCode());
	}
	
	public int updateImage(StaffModel model) {
		String sql = "update staff set image=? where code = ?";
		System.out.println(model.getImage()+"//////////");
		return JDBCUtil.update(sql, model.getImage(),model.getCode());

	}

}
